package HashPackage2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class intersection_ {

  public int[] intersection(int[] nums1, int[] nums2) {
    HashMap<Integer,Integer> map = new HashMap<>();
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < nums1.length; i++) {
      Integer value = map.get(nums1[i]);
      if (value == null){
        map.put(nums1[i], 1);
      }else{
        map.put(nums1[i],value + 1);
      }
    }
    for (int i = 0; i < nums2.length; i++) {
      if(map.containsKey(nums2[i]) && !list.contains(nums2[i])){
        list.add(nums2[i]);
      }
    }
    int[] result = new int[list.size()];
    int index = 0;
    for (int ele : list) {
      result[index++] = ele;
    }
    return result;
  }
}
